<template>
  <section class="filter-section">
    <div class="section-header">
      <h2 class="section-title">
        <span class="title-icon">📋</span>
        筛选条件设置
      </h2>
      <div class="section-description">
        选择级联分类，系统将自动提取最深层级的值作为表格请求参数
      </div>
    </div>

    <div class="filter-content">
      <!-- 级联选择器 -->
      <div class="filter-group">
        <label class="filter-label">
          <span class="label-text">组件分类筛选</span>
          <span class="label-required">*</span>
        </label>
        <div class="cascader-wrapper">
          <CascaderSelect
            :model-value="selectedValue"
            :options="options"
            placeholder="请选择要筛选的组件分类"
            @update:model-value="handleValueUpdate"
            @change="handleCascaderChange"
          />
        </div>
      </div>

      <!-- 选中信息展示 -->
      <transition name="fade-slide" appear>
        <div v-if="selectedValue.length > 0" class="selected-info">
          <div class="info-header">
            <h3 class="info-title">
              <span class="info-icon">🎯</span>
              当前选中信息
            </h3>
            <button 
              class="clear-btn"
              @click="clearSelection"
              :disabled="loading"
            >
              <span class="clear-icon">🗑️</span>
              清空
            </button>
          </div>

          <div class="info-content">
            <div class="info-grid">
              <InfoItem
                label="选择路径"
                :value="selectedLabels.join(' → ')"
                icon="🛤️"
                type="path"
              />
              <InfoItem
                label="选择深度"
                :value="`第 ${selectedValue.length} 层级`"
                icon="📊"
                type="level"
              />
              <InfoItem
                label="请求参数"
                :value="requestParam"
                icon="🔑"
                type="param"
                :highlighted="true"
              />
              <InfoItem
                label="参数类型"
                value="String (name字段)"
                icon="🏷️"
                type="type"
              />
            </div>

            <!-- 请求预览 -->
            <div class="request-preview">
              <h4 class="preview-title">
                <span class="preview-icon">🔧</span>
                API 请求预览
              </h4>
              <div class="request-details">
                <div class="request-item">
                  <span class="request-label">URL:</span>
                  <code class="request-value">/api/table/data</code>
                </div>
                <div class="request-item">
                  <span class="request-label">Method:</span>
                  <code class="request-value">POST</code>
                </div>
                <div class="request-item">
                  <span class="request-label">Body:</span>
                  <code class="request-value">{{ formatRequestBody() }}</code>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>

      <!-- 空状态提示 -->
      <div v-if="selectedValue.length === 0" class="empty-state">
        <div class="empty-icon">🎯</div>
        <div class="empty-text">请选择筛选条件</div>
        <div class="empty-hint">选择后将自动加载对应的表格数据</div>
      </div>
    </div>
  </section>
</template>

<script setup lang="ts">
import { computed, toRefs } from 'vue'
import CascaderSelect from '../CascaderSelect.vue'
import InfoItem from './InfoItem.vue'
import type { CascaderOption, FilterSectionProps, FilterSectionEmits } from '../types/app'

// Props 定义
interface Props {
  options: CascaderOption[]
  selectedValue: string[]
  loading: boolean
}

const props = withDefaults(defineProps<Props>(), {
  loading: false
})

// Emits 定义
const emit = defineEmits<{
  'filter-change': [value: string[], selectedOptions: CascaderOption[]]
  'update:selectedValue': [value: string[]]
}>()

// 解构 props
const { options, selectedValue, loading } = toRefs(props)

// 计算属性
const requestParam = computed<string>(() => {
  if (!selectedValue.value.length) return ''
  return selectedValue.value[selectedValue.value.length - 1]
})

const selectedLabels = computed<string[]>(() => {
  const labels: string[] = []
  let current = options.value

  for (const name of selectedValue.value) {
    const found = current.find(option => option.name === name)
    if (!found) break
    labels.push(found.nameCn)
    current = found.children || []
  }

  return labels
})

// 方法
const handleValueUpdate = (value: string[]): void => {
  emit('update:selectedValue', value)
}

const handleCascaderChange = (value: string[], selectedOptions: CascaderOption[]): void => {
  emit('filter-change', value, selectedOptions)
}

const clearSelection = (): void => {
  emit('update:selectedValue', [])
}

const formatRequestBody = (): string => {
  const body = {
    category: requestParam.value,
    page: 1,
    pageSize: 20,
    timestamp: new Date().toISOString()
  }
  return JSON.stringify(body, null, 2)
}
</script>

<style scoped>
.filter-section {
  background: #ffffff;
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  border: 1px solid #e9ecef;
  margin-bottom: 24px;
}

.section-header {
  margin-bottom: 28px;
  padding-bottom: 20px;
  border-bottom: 2px solid #f1f3f5;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px 0;
  font-size: 1.4rem;
  font-weight: 700;
  color: #2c3e50;
}

.title-icon {
  font-size: 1.5rem;
}

.section-description {
  color: #6c757d;
  font-size: 0.95rem;
  line-height: 1.5;
  margin-left: 32px;
}

.filter-content {
  position: relative;
}

.filter-group {
  margin-bottom: 24px;
}

.filter-label {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 12px;
  font-weight: 600;
  color: #374151;
  font-size: 0.95rem;
}

.label-text {
  flex: 1;
}

.label-required {
  color: #ef4444;
  font-weight: 700;
}

.cascader-wrapper {
  max-width: 500px;
}

/* 选中信息样式 */
.selected-info {
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border-radius: 12px;
  padding: 24px;
  margin-top: 20px;
  border: 1px solid #cbd5e1;
}

.info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.info-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  color: #1e40af;
}

.info-icon {
  font-size: 1.2rem;
}

.clear-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  color: #64748b;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.clear-btn:hover:not(:disabled) {
  background: #e2e8f0;
  border-color: #94a3b8;
  color: #475569;
}

.clear-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.clear-icon {
  font-size: 1rem;
}

.info-content {
  space-y: 20px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

/* 请求预览样式 */
.request-preview {
  background: #1e293b;
  border-radius: 8px;
  padding: 16px;
  color: #e2e8f0;
}

.preview-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px 0;
  font-size: 1rem;
  font-weight: 600;
  color: #60a5fa;
}

.preview-icon {
  font-size: 1.1rem;
}

.request-details {
  space-y: 8px;
}

.request-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.request-label {
  min-width: 60px;
  font-weight: 500;
  color: #94a3b8;
}

.request-value {
  flex: 1;
  background: #0f172a;
  padding: 4px 8px;
  border-radius: 4px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.85rem;
  line-height: 1.4;
  color: #22c55e;
  white-space: pre-wrap;
  word-break: break-all;
}

/* 空状态样式 */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #9ca3af;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 12px;
  opacity: 0.6;
}

.empty-text {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 6px;
  color: #6b7280;
}

.empty-hint {
  font-size: 0.9rem;
  color: #9ca3af;
}

/* 过渡动画 */
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: all 0.3s ease;
}

.fade-slide-enter-from {
  opacity: 0;
  transform: translateY(-10px);
}

.fade-slide-leave-to {
  opacity: 0;
  transform: translateY(10px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .filter-section {
    padding: 20px;
  }
  
  .section-title {
    font-size: 1.2rem;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .info-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .clear-btn {
    align-self: flex-end;
  }
  
  .cascader-wrapper {
    max-width: none;
  }
}
</style>
